package com.example.myapplication;

import android.annotation.SuppressLint;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.myapplication.database.IconDBHelper;
import com.example.myapplication.enity.IconInfo;

import java.util.List;

public class SearchActivity extends AppCompatActivity {
    private IconDBHelper mDBHelper;
    private GridLayout gl_channel2;
    private ScrollView scrollView2;
    private Button back_button;
    private String name;
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_search);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        name = getIntent().getStringExtra("name");
        scrollView2 = findViewById(R.id.scrollView2);
        gl_channel2 = findViewById(R.id.gl_channel2);
        back_button = findViewById(R.id.back);

        mDBHelper = IconDBHelper.getInstance(this);
        mDBHelper.openReadLink();

        back_button.setOnClickListener(v -> {
            finish();
        });

        scrollView2.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
            @Override
            public void onScrollChanged() {
                int scrollY = scrollView2.getScrollY();
                int screenHeight = scrollView2.getHeight();

                for (int i = 0; i < gl_channel2.getChildCount(); i++) {
                    View child = gl_channel2.getChildAt(i);
                    if (child instanceof LinearLayout) {
                        int childTop = child.getTop();
                        int childBottom = child.getBottom();
                        // 判断是否在可视范围内
                        if (childTop < scrollY + screenHeight && childBottom > scrollY) {
                            // 在可视范围内，加载图片
                            ImageView img = child.findViewById(R.id.img);
                            TextView name = child.findViewById(R.id.name);
                            img.setImageURI(Uri.parse((String) img.getTag()));
                            name.setText((String) name.getTag());
                        }
                        else{
                            ImageView img = child.findViewById(R.id.img);
                            img.setImageURI(null);
                        }
                    }
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        getIcons(name);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mDBHelper.closeLink();
    }

    private void getIcons(String name) {

        List<IconInfo> list = mDBHelper.queryIconInfoByName(name);
        // 移除下面的所有子视图
        gl_channel2.removeAllViews();
        Log.i("1", String.valueOf(list.size()));
        int i = 0;
        for (IconInfo info : list) {
            // 获取布局文件item_icon.xml的根视图
            View view = LayoutInflater.from(this).inflate(R.layout.item_icon, null);
            ImageView img = view.findViewById(R.id.img);
            TextView tv_name = view.findViewById(R.id.name);
            if(i <= 8){
                img.setImageURI(Uri.parse(info.path));
                tv_name.setText(info.name);
                i ++;
            }
            // 给控件设置值
            img.setTag(info.path);
            tv_name.setTag(info.name);
            // 把图标视图添加到网格布局
            gl_channel2.addView(view);

        }
    }
}